/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package verification;

import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author NATHAN
 */
public class EmailFieldVerifier extends AbstractValidator {

  private int minLength, maxLength;

  public EmailFieldVerifier(JPanel parent, JComponent c, String message, int minLength, int maxLength) {
    super(parent, c, message);
    this.minLength = minLength;
    this.maxLength = maxLength;
  }

  @Override
  protected boolean validationCriteria(JComponent c) {
    String email = ((JTextField) c).getText();
    email = email.trim();
    // empty check
    if (email.length() == 0) {
      return false;
    }
    // format check
    if (!email.matches("[a-zA-Z0-9]+[a-zA-Z0-9.-_]*@[a-zA-Z0-9]+[a-zA-Z0-9.-_]*.[a-zA-Z0-9]+$")) {
      setMessage("Not a valid email. example@domain.com");
      return false;
    }
    // length check
    if (!(email.length() >= minLength && email.length() <= maxLength)) {
      setMessage("Must be between " + minLength + " and " + maxLength + " characters long.");
      return false;
    }

    return true;
  }
}
